<?php

namespace App\Http\Controllers;

use App\Member;
use Illuminate\Http\Request;

class TestController extends Controller
{
    //
     public function index(Request $request){
         //分页获取数据
         $members = Member::paginate(20);
         return view('test.index',[
             'members' => $members
         ]);
     }

    public function add(Request $request){
        if($request->isMethod('POST')){
            //1.控制器验证
//            $this->validate($request,[
//                'Member.account' => 'required|min:2|max:64',
//               'Member.password' => 'required|min:6|max:20',
//               'Member.sex' => 'required|integer'
//            ],[
//                'required' => ':attribute 为必填项',
//                'min' => ':attribute 长度不符合要求',
//                'max' => ':attribute 长度不符合要求',
//                'integer' => ':attribute 必须为整数',
//            ],[
//                'Member.account' => '帐号',
//                'Member.password' => '密码',
//                'Member.sex' => '性别',
//            ]);
//          2.Validator类验证
            $validator = \Validator::make($request->input(),[
                'Member.account' => 'required|min:2|max:64',
                'Member.password' => 'required|min:6|max:20',
                'Member.sex' => 'required|integer'
            ],[
                'required' => ':attribute 为必填项',
                'min' => ':attribute 长度不符合要求',
                'max' => ':attribute 长度不符合要求',
                'integer' => ':attribute 必须为整数',
            ],[
                'Member.account' => '帐号',
                'Member.password' => '密码',
                'Member.sex' => '性别',
            ]);
            if($validator->fails()){
                return redirect()->back()->withErrors($validator)->withInput();
            }


            $Member = $request->input('Member');
            if(Member::create($Member)){
                return redirect('member/index')->with('success','新增成功');
            }else{
                return redirect()->back();
            }
        }
        return view('test.add');
    }


    public function delete($id){
        if(Member::destroy($id)){
            return redirect('member/index')->with('success','删除成功');
        }else{
            return redirect()->back();
        }
    }
}
